package com.wen.controller.system;

import com.wen.common.utils.ResponseResult;
import com.wen.system.service.PermissionService;
import com.wen.common.pojo.system.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询所有菜单
     */
    @GetMapping("/all")
    public ResponseResult<?> all() {
        List<Permission> allPermission = permissionService.getAllPermission();
        return ResponseResult.success(allPermission);
    }

    /**
     * 权限分页查询
     */
    @GetMapping("/permissionPage")
    public ResponseResult<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "2") Integer pageSize,
                                      @RequestParam(defaultValue = "") String search) {

        return ResponseResult.success(permissionService.findPermissionPage(pageNum, pageSize, search));
    }
    @GetMapping("/list")
    public ResponseResult<?> menuList(Permission param) {
        return permissionService.selectMenuList(param);
    }

    /**
     * 增加菜单
     */
    @PostMapping
    public ResponseResult<?> add(@RequestBody Permission permission) {
        permissionService.add(permission);
        return ResponseResult.success(permission);
    }
    /**
     * 获取菜单
     */
    @GetMapping("/get/{id}")
    public ResponseResult<?> getMenu(@PathVariable("id") Long menuId) {
        Permission menu = permissionService.getMenuById(menuId);
        return ResponseResult.success(menu);
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{id}")
//    @RequiresPermissions("menu:图标管理111")
//    @RequiresRoles({"jue"})
    public ResponseResult<?> delete(@PathVariable("id") Long id) {
        permissionService.delete(id);
        return ResponseResult.success();
    }

    /**
     * 修改菜单
     */
    @PutMapping
    public ResponseResult<?> update(@RequestBody Permission permission) {
        permissionService.update(permission);
        return ResponseResult.success(permission);
    }

    /**
     * 所有菜单的树
     */
    @GetMapping("/allTree")
    public ResponseResult<?> getAllTree() {
        List<Permission> allPermission = permissionService.getAllPermission();
        List<Permission> all = permissionService.getMenuTree(allPermission, 0);

        return ResponseResult.success(all);
    }
}
